/*
 * ScreenUI.c
 *
 *  Created on: May 23, 2021
 *      Author: user, Rupert
 */

#include "ScreenUI.h"

#include "Screen.h"
#include "string.h"
#include "stdio.h"
#include "Temperature.h"
#include "RTClock.h"

#define START_UP_HOUR 14
#define START_UP_MINUTE 0
#define START_UP_SECOND 0

char time_str[20] = "2021-05-24 14:00:00";

#define TEMP_INF 999
typedef struct AlarmLogElement
{
	uint8_t order;
	char time[20];
	float temp;
} AlarmLogElement;

char alarm_log_output_text[32];
char temp_str[6];

AlarmLogElement alarm_log[4] =
{
{ 1, "-------------------", TEMP_INF },
{ 2, "-------------------", TEMP_INF },
{ 3, "-------------------", TEMP_INF },
{ 4, "-------------------", TEMP_INF } };

static void FloatToStr(char* target_str, float value)
{
	int head = (int)value;
	int point = (int)(value - head) * 10;
    sprintf(target_str, "%3d.%1d", head, point);
}

void DisplayPresentTemp(void)
{
	float present_temp;
	present_temp = getTemperature();
	displayNum(TempRT_NumBoxAddr, present_temp);
}

void DisplayTempLimit(void)
{
	float temp_limit;
	temp_limit = getTempLimit();
	displayNum(TempLimit_NumBoxAddr, temp_limit);
}

void DisplayRTClock(void)
{
	ClockStructure clk;
	getRTClock(&clk);
	time_str[11] = (clk.minutes + START_UP_MINUTE) / 60 / 10 + '0';
	time_str[12] = (clk.minutes + START_UP_MINUTE) / 60 % 10 + '0';
	time_str[14] = (clk.minutes + START_UP_MINUTE) % 60 / 10 + '0';
	time_str[15] = (clk.minutes + START_UP_MINUTE) % 60 % 10 + '0';
	time_str[17] = clk.seconds / 10 + '0';
	time_str[18] = clk.seconds % 10 + '0';
	displayText(Time_TextBoxAddr, time_str, 19);
}

void UpdateTempLimit(void)
{
	ButtonType recent_button;
	recent_button = recieveButtonPushed();
	float temp_limit;
	temp_limit = getTempLimit();
	switch (recent_button)
	{
	case ButtonDecrease:
		setTempLimit(temp_limit - 5);
		break;
	case ButtonIncrease:
		setTempLimit(temp_limit + 5);
		break;
	default:
		break;
	}
}

void DisplayAlarmLog(void)
{
	for (uint8_t i = 0; i < 4; i++)
	{
		switch (alarm_log[i].order)
		{
		case 1:
			if (alarm_log[i].temp != TEMP_INF)
			{
				FloatToStr(temp_str, alarm_log[i].temp);
				strcpy(alarm_log_output_text, alarm_log[i].time);
				strcat(alarm_log_output_text, " ");
				strcat(alarm_log_output_text, temp_str);
				displayText(AlarmLog1_TextBoxAddr, alarm_log_output_text, 31);
			}
			else
			{
				displayText(AlarmLog1_TextBoxAddr,
						"-------------------------------", 31);
			}
			break;
		case 2:
			if (alarm_log[i].temp != TEMP_INF)
			{
				FloatToStr(temp_str, alarm_log[i].temp);
				strcpy(alarm_log_output_text, alarm_log[i].time);
				strcat(alarm_log_output_text, " ");
				strcat(alarm_log_output_text, temp_str);
				displayText(AlarmLog2_TextBoxAddr, alarm_log_output_text, 31);
			}
			else
			{
				displayText(AlarmLog2_TextBoxAddr,
						"-------------------------------", 31);
			}
			break;
		case 3:
			if (alarm_log[i].temp != TEMP_INF)
			{
				FloatToStr(temp_str, alarm_log[i].temp);
				strcpy(alarm_log_output_text, alarm_log[i].time);
				strcat(alarm_log_output_text, " ");
				strcat(alarm_log_output_text, temp_str);
				displayText(AlarmLog3_TextBoxAddr, alarm_log_output_text, 31);
			}
			else
			{
				displayText(AlarmLog3_TextBoxAddr,
						"-------------------------------", 31);
			}
			break;
		case 4:
			if (alarm_log[i].temp != TEMP_INF)
			{
				FloatToStr(temp_str, alarm_log[i].temp);
				strcpy(alarm_log_output_text, alarm_log[i].time);
				strcat(alarm_log_output_text, " ");
				strcat(alarm_log_output_text, temp_str);
				displayText(AlarmLog4_TextBoxAddr, alarm_log_output_text, 31);
			}
			else
			{
				displayText(AlarmLog4_TextBoxAddr,
						"-------------------------------", 31);
			}
			break;
		}
	}
}

void addAlarmLog(float temp)
{
	static ClockStructure last_clk = {0, 0, 0};
	ClockStructure clk;
	getRTClock(&clk);
	if((clk.minutes - last_clk.minutes) * 60 + (clk.seconds - last_clk.seconds) < 5)
	{
		return;
	}
	for (uint8_t i = 0; i < 4; i++)
	{
		if (alarm_log[i].order < 4)
		{
			alarm_log[i].order++;
		}
		else
		{
			time_str[11] = (clk.minutes + START_UP_MINUTE) / 60 / 10 + '0';
			time_str[12] = (clk.minutes + START_UP_MINUTE) / 60 % 10 + '0';
			time_str[14] = (clk.minutes + START_UP_MINUTE) % 60 / 10 + '0';
			time_str[15] = (clk.minutes + START_UP_MINUTE) % 60 % 10 + '0';
			time_str[17] = clk.seconds / 10 + '0';
			time_str[18] = clk.seconds % 10 + '0';
			strcpy(alarm_log[i].time, time_str);
			alarm_log[i].temp = temp;
			alarm_log[i].order = 1;
		}
	}
	last_clk.minutes = clk.minutes;
	last_clk.seconds = clk.seconds;
	last_clk.micro_seconds = clk.micro_seconds;
}

void RefreshScreen(void)
{
	UpdateTempLimit();
	DisplayTempLimit();
	DisplayRTClock();
	DisplayPresentTemp();
	DisplayAlarmLog();
}
